package com.sui.bigdata.rtcadmin.util;

import com.sui.bigdata.rtcadmin.exception.FlinkCommonException;
import com.sui.bigdata.rtcadmin.exception.LoginFailException;
import com.sui.bigdata.rtcadmin.exception.TokenInvalidException;
import com.sui.universal.client.UniversalLoginClient;
import com.sui.universal.entity.Person;
import com.sui.universal.entity.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Enumeration;

/**
 * @author YongChen
 * @date 2020/7/13 10:46
 * @description
 * @email yong_chen@sui.com
 */
@Component
public class CheckUtils {

    @Autowired
    private  UniversalLoginClient universalLoginClient;

    public  void chekcToken(Enumeration<String> authorization) throws Exception{
        if (!authorization.hasMoreElements()){
           throw  new FlinkCommonException("authorization should not be null");
        }
        String token = authorization.nextElement();
        if ("admin".equals(token)){
            return;
        }
        ResponseEntity<Person> responseEntity =  universalLoginClient.info(token);
        if (responseEntity.getCode() != 200){
            throw new TokenInvalidException();
        }
    }

    public  void chekcLogin(ResponseEntity<String> responseEntity){
        if (responseEntity.getCode() !=200){
            throw new LoginFailException();
        }
    }
}
